package com.fangfei.runtest.controller;

import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

    @RequestMapping("/test")
    public String test() {
        try {
            File path = new File(ResourceUtils.getURL("classpath:gdb-remote.yml").getPath());
            Client client = Cluster.build(path).create().connect();
            client.init();
            String dsl = "g.addV(test).property(_id, 1)";
            Map<String,Object> parameters = new HashMap<>();
            parameters.put("a","b");
            parameters.put("c","d");
            parameters.put("e","f");
            ResultSet results = client.submit(dsl,parameters);
            List<Result> result = results.all().join();
            result.forEach(p -> System.out.println(p.getObject()));
            client.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return "success";
    }

    @RequestMapping("/show")
    public String show() {
        try {
            File path = new File(ResourceUtils.getURL("classpath:gdb-remote.yml").getPath());
            Client client = Cluster.build(path).create().connect();
            client.init();
            String dsl = "g.V()";
            ResultSet results = client.submit(dsl);
            List<Result> result = results.all().get();
            result.forEach(p -> System.out.println(p.getObject()));
            client.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return "success";
    }

}